<?php

// 每天统计vip用户控制器

namespace app\api\controller;

use app\api\controller\Base;
use think\Db;

class DayUser extends Base
{

    public function user($class = 'outside')
    {

        try {
            // 只允许命令行访问
            if (PHP_SAPI != 'cli') {
                if ($class != 'inside') {
                    return ['code' => 999, 'msg' => '404'];
                }
            }

            debug('begin');

            // 统计时间（今天跑以前的数据）
            $query_time = date('Y-m-d');

            $create_time = strtotime($query_time) - 86400 * 10;
            $update_time = strtotime($query_time);

            $map = [];
            Db::name('payment_merchant_config')
                ->where($map)
                ->chunk(100, function ($all) use ($create_time, $update_time) {
                    foreach ($all as $single) {

                        if ($single['status'] == 1) {

                            $temp_map = [];
                            $temp_map[] = ['merchant_id', '=', $single['id']];
                            $temp_map[] = ['user_id', '<>', 0];
                            $temp_map[] = ['status', '=', 2];
                            $temp_map[] = ['create_time', ['>=', $create_time], ['<', $update_time]];

                            $all_user_order = Db::name('payment_order')
                                ->where($temp_map)
                                ->field('merchant_id,user_id,app_id,count(user_id) as counts')
                                ->group('user_id')
                                ->having('count(user_id)>=50')
                                ->select();

                            if ($all_user_order) {

                                foreach ($all_user_order as $key => $value) {

                                    // 用户白名单是否存在
                                    $user_whitelist = Db::name('payment_user_whitelist')
                                        ->where('merchant_id', $value['merchant_id'])
                                        ->where('user_id', $value['user_id'])
                                        ->where('app_id', $value['app_id'])
                                        ->find();
                                    if ($user_whitelist) {
                                        continue;
                                    }

                                    // 用户黑名单是否存在
                                    $user_blacklist = Db::name('payment_user_blacklist')
                                        ->where('merchant_id', $value['merchant_id'])
                                        ->where('user_id', $value['user_id'])
                                        ->where('app_id', $value['app_id'])
                                        ->find();
                                    if ($user_blacklist) {
                                        continue;
                                    }

                                    // 用户灰名单是否存在
                                    $user_graylist = Db::name('payment_user_graylist')
                                        ->where('merchant_id', $value['merchant_id'])
                                        ->where('user_id', $value['user_id'])
                                        ->where('app_id', $value['app_id'])
                                        ->find();
                                    if ($user_graylist) {
                                        Db::name('payment_user_graylist')
                                            ->where('merchant_id', $value['merchant_id'])
                                            ->where('user_id', $value['user_id'])
                                            ->where('app_id', $value['app_id'])
                                            ->inc('source_count')
                                            ->update(['update_time' => time()]);
                                    } else {
                                        $user_data['merchant_id'] = $value['merchant_id'];
                                        $user_data['user_id'] = $value['user_id'];
                                        $user_data['app_id'] = $value['app_id'];
                                        $user_data['count'] = $value['counts'];
                                        $user_data['source_count'] = 1;
                                        $user_data['class'] = 0;
                                        $user_data['create_time'] = $create_time;
                                        $user_data['update_time'] = $update_time;

                                        Db::name('payment_user_graylist')
                                            ->insert($user_data);
                                    }
                                }
                            }
                            unset($all_user_order);
                        }
                        unset($single);
                    }
                }, 'id', 'asc');

            Db::name('payment_merchant_config')
                ->where($map)
                ->chunk(100, function ($all) use ($create_time, $update_time) {
                    foreach ($all as $single) {

                        if ($single['status'] == 1) {

                            $temp_map = [];
                            $temp_map[] = ['merchant_id', '=', $single['id']];
                            $temp_map[] = ['user_id', '<>', 0];
                            $temp_map[] = ['status', '=', 1];
                            $temp_map[] = ['create_time', ['>=', $create_time], ['<', $update_time]];

                            $all_user_black_order = Db::name('payment_order')
                                ->where($temp_map)
                                ->field('merchant_id,user_id,app_id,count(user_id) as counts')
                                ->group('user_id')
                                ->having('count(user_id)>=100')
                                ->select();

                            if ($all_user_black_order) {
                                foreach ($all_user_black_order as $key => $value) {

                                    // 用户黑名单是否存在
                                    $user_blacklist = Db::name('payment_user_blacklist')
                                        ->where('merchant_id', $value['merchant_id'])
                                        ->where('user_id', $value['user_id'])
                                        ->where('app_id', $value['app_id'])
                                        ->find();
                                    if ($user_blacklist) {
                                        continue;
                                    }

                                    // 用户灰名单是否存在
                                    $user_graylist = Db::name('payment_user_graylist')
                                        ->where('merchant_id', $value['merchant_id'])
                                        ->where('user_id', $value['user_id'])
                                        ->where('app_id', $value['app_id'])
                                        ->find();
                                    if ($user_graylist) {
                                        Db::name('payment_user_graylist')
                                            ->where('merchant_id', $value['merchant_id'])
                                            ->where('user_id', $value['user_id'])
                                            ->where('app_id', $value['app_id'])
                                            ->inc('source_count')
                                            ->update(['update_time' => time(), 'class' => 1]);
                                    } else {
                                        $user_data['merchant_id'] = $value['merchant_id'];
                                        $user_data['user_id'] = $value['user_id'];
                                        $user_data['app_id'] = $value['app_id'];
                                        $user_data['count'] = $value['counts'];
                                        $user_data['source_count'] = 1;
                                        $user_data['class'] = 1;
                                        $user_data['create_time'] = $create_time;
                                        $user_data['update_time'] = $update_time;

                                        // Db::name('payment_user_graylist')->insert($user_data);
                                    }
                                }
                            }
                            unset($all_user_black_order);
                        }
                        unset($single);
                    }
                }, 'id', 'asc');

            debug('end');
            payment_error_log(750001, debug('begin', 'end', 2), debug('begin', 'end', 'm'));
        } catch (\Exception $e) {
            payment_error_log(750000, $e->getMessage());
            return ['code' => 999, 'msg' => '404'];
        }
    }
}
